package com.aispeech.testcustom.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.aispeech.aios.sdk.AIOSForCarSDK;
import com.aispeech.aios.sdk.listener.AIOSReadyListener;
import com.aispeech.aios.sdk.manager.AIOSCustomizeManager;

import com.aispeech.testcustom.TiApplication;
import com.aispeech.testcustom.i.TiConst;
import com.aispeech.testcustom.i.CustomResultCallBack;
import com.aispeech.testcustom.listener.CustomizeListener;

public class AiosForCarReceiver extends BroadcastReceiver {
    private static Message   msg;
    private static Messenger messenger;

    public AiosForCarReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.d("test", "onReceive: --" + intent.getAction());
        Bundle extras = intent.getExtras();

        if (null != extras) {
            Messenger mes = (Messenger) extras.get(TiConst.CALL_BACK_RESULT);
            if (null != mes){
                messenger=mes;
            }
        }

        String action = intent.getAction();
        if ("aios.intent.action.init_sdk_broadcast".equals(action)) {
            //init sdk
            AIOSForCarSDK.initialize(TiApplication.getApplication(), new AIOSReadyListener() {
//                @Override
//                public void onAIOSRebooted() {
//                    Log.d("test", "onAIOSRebooted: in receiver");
//                    onAIOSReady();
//                }

                @Override
                public void onAIOSReady() {
                    Log.d("test", "onAIOSReady: init in receiver");
                    //把当前TiApplication.mlist转成regCommands需要格式
//                    AIOSCustomizeManager.getInstance().regCommands(TiApplication
//                            .convertList4Register());
                    AIOSCustomizeManager.getInstance().registerCustomizeListener(new
                            CustomizeListener(new CustomResultCallBack() {

                        @Override
                        public void setResultBack(String s) {
                            //把识别结果显示在topshow
                            msg = Message.obtain();
                            if (null != msg) {

                                msg.what = TiConst.MSG_REC_RESULT;
                                Log.d("test", "setResultBack: onRecriver call back : s is " + s);
                                msg.obj = s;
                                try {
                                    messenger.send(msg);
                                } catch (RemoteException e) {
                                    e.printStackTrace();
                                    Log.d("test", "setResultBack: 回调失败");
                                }
                            }
                        }
                    }));
                }
            });
            Toast.makeText(TiApplication.getContext(), "广播接受注册完毕", Toast.LENGTH_SHORT).show();
        }else if ("aios.intent.action.application_callback_result".equals(intent.getAction())){
            //application里注册的自定义指令回调在MainActivity里显示
            String s = (String) extras.get("application_result");
            if (!TextUtils.isEmpty(s)){

                Log.d("test", "onReceive: application里注册的自定义指令回调给activity s:" + s);
                msg = Message.obtain();
                if (null != msg) {

                    msg.what = TiConst.MSG_REC_RESULT;
                    msg.obj = s;
                    try {
                        messenger.send(msg);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                        Log.d("test", "setResultBack: 回调失败");
                    }
                }
            }
        }else if ("aios.intent.action.tools_exit".equals(intent.getAction())){
            Log.d("test", "onReceive: getReceiver for exit");
            System.exit(-1);
        }
    }
}
